package com.lx.service.impl;

import com.lx.dao.AddressDao;
import com.lx.entity.Address;
import com.lx.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressDao addressDao;

    @Override
    public List<Address> getAllAddresses(Integer userId) {
        return addressDao.selectAllByUserId(userId);
    }

    @Override
    public Address getAddressById(Integer id) {
        return addressDao.selectById(id);
    }

    @Override
    public boolean addAddress(Address address) {
        // 拼接完整地址
        address.buildAddress();
        return addressDao.insertAddress(address) > 0;
    }

    @Override
    public boolean updateAddress(Address address) {
        address.buildAddress();
        return addressDao.updateAddress(address) > 0;
    }

    @Override
    public boolean deleteAddress(Integer id) {
        return addressDao.deleteAddress(id) > 0;
    }

    @Override
    public boolean setDefaultAddress(Integer userId, Integer addressId)
    {
        addressDao.cancelDefaultAddress(userId);
        return addressDao.setDefaultAddress(userId, addressId) > 0;
    }
}
